package com.foreveross.taskservice.infra.util;

import java.lang.reflect.Method;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.foreveross.taskservice.infra.db.HBaseCellModel;
import com.foreveross.taskservice.infra.entity.DetailInfoEntity;
import com.foreveross.taskservice.infra.entity.StaticHistoryHbaseEntity;
import com.foreveross.taskservice.infra.hbase.IBaseEntity;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;

public class HbaseUtils {

	private static Map<String,Method> detailMethods=Maps.newHashMap();
	static{
		Method[] detailM=DetailInfoEntity.class.getMethods();
		for(Method m:detailM){
			detailMethods.put(m.getName(), m);
		}
	}
	public static <T extends IBaseEntity> void getDetailInfoEntityList(T entity,Set<HBaseCellModel> tmpCells) {
		Iterator<HBaseCellModel> it =tmpCells.iterator();
		Set<DetailInfoEntity> details=Sets.newHashSet();
		DetailInfoEntity [] deStrs=new DetailInfoEntity [tmpCells.size()];
		while(it.hasNext()){
			HBaseCellModel hc=it.next();
			try {
				
				String family=hc.getFamily();
				String qua=hc.getQualifier();
				Object value=hc.getValue();
				if("details".equals(family)){
					//执行反射
					String quaStr=qua.split("_")[0];
					int i=Integer.valueOf(qua.split("_")[1]);
					String methodSet="set"+quaStr.substring(0,1).toUpperCase()+quaStr.substring(1);
					Method method=detailMethods.get(methodSet);
					if(methodSet.equalsIgnoreCase("setActionType")){
						value=Integer.parseInt(value.toString());
					} 
					if(methodSet.equalsIgnoreCase("setTimestamp")){
						value=Long.parseLong(value.toString());
					}
					if(null !=method){
						if(deStrs[i] ==null){
							deStrs[i]=new DetailInfoEntity();
						}
						method.invoke(deStrs[i], value);
					}
				}
				
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		for(int i=0;i<deStrs.length;i++){
			DetailInfoEntity en=deStrs[i];
			if(en !=null)
				details.add(en);
		}
		
		
		if(details.size()>0){
			
			
			
			timeComparator co=new timeComparator();
			List<DetailInfoEntity> del=new ArrayList<DetailInfoEntity>();
			for(DetailInfoEntity de:details){
				del.add(de);
			}
//			Collections.sort(del,co);
			
			Collections.sort(del, new Comparator<DetailInfoEntity>(){
				SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS" );
				@Override
				public int compare(DetailInfoEntity o1,DetailInfoEntity o2) {
					try {
						Date dt1 = df.parse(o1.getTimestampStr());
						Date dt2 = df.parse(o2.getTimestampStr());
						return dt1.compareTo(dt2);
					}catch (Exception e) {
						e.printStackTrace();
					}
					return -1;
				}
				
			});
			//根据时间排序
			Set<DetailInfoEntity> set=Sets.newHashSet();
			if(del.size()>0){
				for(DetailInfoEntity del2:del){
					set.add(del2);
				}
				((StaticHistoryHbaseEntity) entity).setDetails(set);
			}
		}
	}

}

//比较器，对象根据日期时间排序
class timeComparator implements Comparator<DetailInfoEntity>{
	@Override
	public int compare(DetailInfoEntity arg0, DetailInfoEntity arg1) {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS" );
		try {
			Date dt1 = df.parse(arg0.getTimestampStr());
			Date dt2 = df.parse(arg1.getTimestampStr());
			if(dt1 !=null && dt2!=null){
				
				return dt2.compareTo(dt1);
			}else{
				return 0;
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return -1;
	}
}



